package logic

import (
	"context"

	"go-zero_try/mall/order/rpc/pb/internal/svc"
	"go-zero_try/mall/order/rpc/pb/pb"
	"go-zero_try/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelLogic {
	return &DelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DelLogic) Del(in *pb.DelAndQryReq) (*pb.DelAndQryReps, error) {
	u1, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UID)
	msg := ""
	u2 := model.Users{
		Uid:      "0",
		Username: "unknown",
		Age:      "-1",
	}
	if err == nil {
		err := l.svcCtx.UserModel.Delete(l.ctx, in.UID)
		if err == nil {
			msg = "删除成功"
			u2.Username = u1.Username
			u2.Age = u1.Age
			u2.Uid = u1.Uid
		} else {
			msg = "删除失败"
		}
	} else {
		msg = "查无此人"
	}
	return &pb.DelAndQryReps{
		Msg:  msg,
		UID:  u2.Uid,
		Name: u2.Username,
		Age:  u2.Age,
	}, nil
}
